<?php
require_once "TcAI.Exception.php";
require_once "TcAI.Config.php";
require_once "TcAI.Tool.php";
class TcAIApi
{
    private $apiArr = array(
        'faceMerge', //人脸融合
        'faceCosmetic', //人脸美妆
        'faceDecoration', //人脸变妆
        'imgFilter', //滤镜
        'faceSticker', //大头贴
        'faceAge', //颜龄检测
    );
    private $result;
    private $error_code;
    private $error_msg;

    public function __construct($apiName, $image, $param)
    {
        $result = array(
            'status' => 0,
            'msg' => '',
        );
        if (in_array($apiName, $this->apiArr)) {
            $_image = $this->$apiName($image, $param);
            if ($_image === false) {
                $result['status'] = -2;
                $result['msg'] = 'tencet is not connected';
            } else if ($_image === 0) {
                $result['status'] = $this->error_code;
                $result['msg'] = $this->error_msg;
            } else {
                $result['status'] = 0;
                $result['msg'] = 'ok';
                $result['image'] = $_image;
            }
        } else {
            $result['status'] = -1;
            $result['msg'] = 'api is not allowed';
        }
        $this->result = $result;
    }
    public function start()
    {
        return $this->result;
    }

    // 人脸融合接口
    private function faceMerge($image, $model = 1)
    {

        $parameters = array(
            'app_id' => TcAIConfig::APPID,
            'time_stamp' => strval(time()),
            'nonce_str' => strval(rand()),
            'model' => $model,
            'image' => $image,
            'sign' => '',
        );
        $parameters['sign'] = TcAITool::getReqSign($parameters); //添加签名
        $result = TcAITool::doHttpPost(TcAIConfig::API_URL_PREFIX . TcAIConfig::FACEMERGE_URL, $parameters);
        if (false === $result) {
            return false;
        }
        $json = json_decode($result, true);
        if (empty($json) || intval($json['ret']) != 0) {
            // error
            $this->error_code = $json['ret'];
            $this->error_msg = $json['msg'];
            return 0;
        }
        return $json['data']['image'];
    }

    // 人脸美妆接口
    private function faceCosmetic($image, $cosmetic = 1)
    {
        $parameters = array(
            'app_id' => TcAIConfig::APPID,
            'time_stamp' => strval(time()),
            'nonce_str' => strval(rand()),
            'cosmetic' => $cosmetic,
            'image' => $image,
            'sign' => '',
        );
        $parameters['sign'] = TcAITool::getReqSign($parameters); //添加签名
        $result = TcAITool::doHttpPost(TcAIConfig::API_URL_PREFIX . TcAIConfig::FACECOSMETIC_URL, $parameters);
        if (false === $result) {
            return false;
        }
        $json = json_decode($result, true);
        if (empty($json) || intval($json['ret']) != 0) {
            // error
            $this->error_code = $json['ret'];
            $this->error_msg = $json['msg'];
            return false;
        }
        return $json['data']['image'];
    }

    // 人脸变妆接口
    private function faceDecoration($image, $decoration = 1)
    {
        $parameters = array(
            'app_id' => TcAIConfig::APPID,
            'time_stamp' => strval(time()),
            'nonce_str' => strval(rand()),
            'decoration' => $decoration,
            'image' => $image,
            'sign' => '',
        );
        $parameters['sign'] = TcAITool::getReqSign($parameters); //添加签名
        $result = TcAITool::doHttpPost(TcAIConfig::API_URL_PREFIX . TcAIConfig::FACEDECORATION_URL, $parameters);
        if (false === $result) {
            return false;
        }
        $json = json_decode($result, true);
        if (empty($json) || intval($json['ret']) != 0) {
            // error
            $this->error_code = $json['ret'];
            $this->error_msg = $json['msg'];
            return false;
        }
        return $json['data']['image'];
    }

    // 滤镜接口
    private function imgFilter($image, $filter = 1)
    {
        $parameters = array(
            'app_id' => TcAIConfig::APPID,
            'time_stamp' => strval(time()),
            'nonce_str' => strval(rand()),
            'filter' => $filter,
            'image' => $image,
            'sign' => '',
        );
        $parameters['sign'] = TcAITool::getReqSign($parameters); //添加签名
        $result = TcAITool::doHttpPost(TcAIConfig::API_URL_PREFIX . TcAIConfig::IMGFILTER_URL, $parameters);
        if (false === $result) {
            return false;
        }
        $json = json_decode($result, true);
        if (empty($json) || intval($json['ret']) != 0) {
            // error
            $this->error_code = $json['ret'];
            $this->error_msg = $json['msg'];
            return false;
        }
        return $json['data']['image'];
    }

    // 大头贴接口
    private function faceSticker($image, $sticker = 2)
    {
        $parameters = array(
            'app_id' => TcAIConfig::APPID,
            'time_stamp' => strval(time()),
            'nonce_str' => strval(rand()),
            'sticker' => $sticker,
            'image' => $image,
            'sign' => '',
        );
        $parameters['sign'] = TcAITool::getReqSign($parameters); //添加签名
        $result = TcAITool::doHttpPost(TcAIConfig::API_URL_PREFIX . TcAIConfig::FACESTICKER_URL, $parameters);
        if (false === $result) {
            return false;
        }
        $json = json_decode($result, true);

        if (empty($json) || intval($json['ret']) != 0) {
            // error
            $this->error_code = $json['ret'];
            $this->error_msg = $json['msg'];
            return false;
        }
        return $json['data']['image'];
    }

    // 颜龄检测接口
    private function faceAge($image)
    {
        $parameters = array(
            'app_id' => TcAIConfig::APPID,
            'time_stamp' => strval(time()),
            'nonce_str' => strval(rand()),
            'image' => $image,
            'sign' => '',
        );
        $parameters['sign'] = TcAITool::getReqSign($parameters); //添加签名
        $result = TcAITool::doHttpPost(TcAIConfig::API_URL_PREFIX . TcAIConfig::FACEAGE_URL, $parameters);
        if (false === $result) {
            return false;
        }
        $json = json_decode($result, true);
        if (empty($json) || intval($json['ret']) != 0) {
            // error
            $this->error_code = $json['ret'];
            $this->error_msg = $json['msg'];
            return false;
        }
        return $json['data']['image'];
    }

    // 文字自动翻译接口
    private function textTrans($text, $type = 0)
    {

        $parameters = array(
            'app_id' => TcAIConfig::APPID,
            'time_stamp' => strval(time()),
            'nonce_str' => strval(rand()),
            'text' => $text,
            'type' => $type,
            'sign' => '',
        );
        $parameters['sign'] = TcAITool::getReqSign($parameters); //添加签名
        $result = TcAITool::doHttpPost(TcAIConfig::API_URL_PREFIX . TcAIConfig::TEXTTRANS_URL, $parameters);
        if (false === $result) {
            $this->error_msg = '请求接口失败~';
            return false;
        }
        $json = json_decode($result, true);
        if (empty($json) || intval($json['ret']) != 0) {
            // error
            $this->error_code = $json['ret'];
            $this->error_msg = $json['msg'];
            return false;
        }
        return $json['data']['trans_text'];
    }

}
